package org.paybiz.core.payplatform.yeePay.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Description:
 * @author  LiChunming
 * @version V1.0 
 * @createDateTime：2010-9-19 下午02:32:44 
 * @Company: MSD. 
 * @Copyright: Copyright (c) 2010
 */
public class HttpClientUtil {
    private static Log log = LogFactory.getLog(HttpClientUtil.class);
    private static HttpClient client;
    private static MultiThreadedHttpConnectionManager multi;
    private static String headerValue = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";

    static {
        // add by wenjl 20120611
        Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443);
        Protocol.registerProtocol("https", myhttps);
        client = new HttpClient();
        multi = new MultiThreadedHttpConnectionManager();
        multi.getParams().setConnectionTimeout(30000);
        multi.getParams().setSoTimeout(30000);
    }

    /**
     * Get方法得到回写值
     * 
     * @param request
     */
    public static String getResponseByGet(String url, NameValuePair[] params) {
        GetMethod method = new GetMethod(url);
        method.setRequestHeader("User-Agent", headerValue);
        if (params != null)
            method.setQueryString(params);
        String rel = "";
        try {
            client.setHttpConnectionManager(multi);
            int status = client.executeMethod(method);
            log.info("##-->http status code:" + status);
            if (status == HttpStatus.SC_OK) {
                BufferedInputStream is = new BufferedInputStream(method.getResponseBodyAsStream());

                BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));
                StringBuffer result = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    result.append(temp);
                    result.append("\n");
                }
                rel = result.toString();
                br.close();
                is.close();
                br = null;
                is = null;
                method.abort();
                method.releaseConnection();
            }
            method = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rel;
    }

    public static String getResponseByPost(String url, NameValuePair[] params) {
        PostMethod method = new PostMethod(url);
        //
        method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "gb2312");
        method.setRequestHeader("User-Agent", headerValue);
        if (params != null) {
            method.setRequestBody(params);
        }
        String rel = "";
        try {
            client.setHttpConnectionManager(multi);
            int status = client.executeMethod(method);
            log.info("##-->http status code:" + status);
            if (status == HttpStatus.SC_OK) {
                BufferedInputStream is = new BufferedInputStream(method.getResponseBodyAsStream());
                BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));
                StringBuffer result = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    result.append(temp);
                    result.append("\n");
                }
                rel = result.toString();
                br.close();
                is.close();
                br = null;
                is = null;
                method.abort();
                method.releaseConnection();
            }
            method = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rel;
    }

}
